//
//  WelcomViewController.swift
//  XinLangWeibo10
//
//  Created by 白利兵 on 15/10/19.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class WelcomViewController: UIViewController {
    // 懒加载背景图片
    private lazy var backgroudImage: UIImageView = UIImageView(imageName: "ad_background")
    // 懒加载用户头像
    private lazy var iconImage: UIImageView = {
        let iconImg = UIImageView(imageName: "avatar_default_big")
        // 剪切成为圆
        iconImg.layer.cornerRadius = 45
        iconImg.layer.masksToBounds = true
        return iconImg
        }()
    private lazy var titleLabel: UILabel = UILabel(title: "欢迎归来", fontSize: 18, color: UIColor.darkGrayColor())
    
    override func loadView() {
        super.loadView()
        view = backgroudImage
        // 设置UI
        setupUI()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 异步加载头像图片
        let account = UserAccountViewModel.shareUserAccount
        iconImage.sd_setImageWithURL(account.avatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    override func viewDidAppear(animated: Bool) {
        // 更新约束
        titleLabel.alpha = 0
        iconImage.snp_updateConstraints { (make) -> Void in
                make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            // 提前进行自动布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.titleLabel.alpha = 1.0
                    print("OK")
                    }, completion: { (_) -> Void in
                        
                        // 发送通知(跳转到主界面)
                        NSNotificationCenter.defaultCenter().postNotificationName(WBNotificationSwithRootView, object: nil)
                })
        }
    }
}
extension WelcomViewController {
    // 设置UI
    private func setupUI() {
        // 添加控件
        view.addSubview(iconImage)
        view.addSubview(titleLabel)
        
        // 添加约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        titleLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_centerX)
            make.top.equalTo(iconImage.snp_bottom).offset(12)
        }
        
    }
}








